package com.inspur.gs.sbssdl.dzyhpgl.dzyhpbf.dzyhpbf.back.bizactions;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.core.LcpUtil;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.api.action.validation.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.api.be.*;
import com.inspur.edp.bef.api.action.VoidActionResult;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.entity.entity.IValuesContainer;

import java.math.BigDecimal;

public class sbssDzyhpBfbfkcSubmitMgrAction extends AbstractManagerAction<VoidActionResult> {
	private String id;
	public sbssDzyhpBfbfkcSubmitMgrAction(IBEManagerContext managerContext, String id) {
		super(managerContext);
		this.id = id;
	}

	private String bfdCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhpbf.dzyhpbf.back.sbssDzyhpBf";
	private String kcCfg = "com.inspur.gs.sbssdl.dzyhpgl.dzyhyu.dzyhyu.back.sbssYhpItem";

	/**
	 * 提交库存报废
	 */
	@Override
	public  void execute() {
		IStandardLcp lcp = LcpUtil.getLcp(bfdCfg);
		IStandardLcp kcLcp = LcpUtil.getLcp(kcCfg);

		IEntityData bfdData = lcp.retrieve(this.id).getData();
		IEntityDataCollection hzdMx = bfdData.getChilds().get("bfmx");

		for (IEntityData mx : hzdMx) {
			submitKc(bfdData,mx,kcLcp);
		}
	}
	private IBusinessEntity getEntity(String dataId) {
		return (IBusinessEntity)super.getBEManagerContext().getEntity(dataId);
	}	private IBEService getMgr() {
		return (IBEService)super.getBEManagerContext().getBEManager();
	}

	private void submitKc(IEntityData hzdData, IEntityData mx, IStandardLcp kcLcp) {
		String mxId = mx.getID();
		int lockNum = Integer.parseInt(mx.getValue("bfsl").toString());
		IValuesContainer kcdh = (IValuesContainer) mx.getValue("kcdh");
		if (kcdh == null || kcdh.getValue("kcdh") == null) {
			return;
		}

		String kcId = kcdh.getValue("kcdh").toString();
		IEntityData kcData = kcLcp.retrieve(kcId).getData();
		int tsl = Integer.parseInt(kcData.getValue("tsl").toString());

		// 检查日志是否已经提交,已经提交则不进行修改
		IEntityDataCollection itemtras = kcData.getChilds().get("itemtras");
		boolean hasItem = itemtras.stream().anyMatch(item -> item.getValue("djMxid") != null && item.getValue("djMxid").toString().equals(mxId) && item.getValue("trasStatus").toString().equals("1"));
		if (hasItem) return;

		// 修改数量
		ModifyChangeDetail kcMcd = new ModifyChangeDetail(kcId);
		kcMcd.putItem("tsl", tsl - lockNum);

		// 修改总价
		BigDecimal sl = new BigDecimal(tsl);
		BigDecimal dj =  (BigDecimal) kcData.getValue("wlDj");
		kcMcd.putItem("zj",sl.multiply(dj));

		itemtras.stream().filter(i-> i.getValue("djMxid") != null && i.getValue("djMxid").toString().equals(mxId))
				.forEach(i->{
					ModifyChangeDetail kcMxMcd = new ModifyChangeDetail(i.getID());
					kcMxMcd.getPropertyChanges().put("trasStatus","1");
					kcMcd.addChildChangeSet("itemtras",kcMxMcd);
				});
		// 修改转出为审核完毕
		kcLcp.modify(kcMcd);

		kcLcp.save();
	}
}